<?php
header("Content-Type: text/html; charset=iso-8859-1");
session_start();
@$ftmp = $_FILES['image']['tmp_name'];
@$oname = $_FILES['image']['name'];
@$fsize = $_FILES['image']['size'];
@$ftype = $_FILES['image']['type'];
$dir = "images/partenaires";
$types = array(
	1=>"gif",
	2=>"jpeg",
	3=>"png",
	4=>"swf",
	5=>"psd",
	6=>"bmp"
	);
$id = (!empty($_SESSION['id'])) ? $_SESSION['id'] : "";
if(IsSet($ftmp) && $id) :
	$type = $types[exif_imagetype($ftmp)];
	@$fname = $id.".".($types[exif_imagetype($ftmp)]);
	$newname = $id.".jpeg";
	if(move_uploaded_file($ftmp,"$dir/".$fname)){
		$filename = $fname;
		list($width, $height) = getimagesize("$dir/".$filename);
		if($width < 180){
			rename("$dir/".$fname,"$dir/".$newname);
		}
		else{
			$new_width = 180;
			$new_height = 180 / $width * $height;
			$image_p = imagecreatetruecolor($new_width, $new_height);
			if($type == "jpeg"){
				$image = imagecreatefromjpeg("$dir/".$filename);
				imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
				imagejpeg($image_p,"$dir/".$newname, 180);
				if($fname != $newname) unlink("$dir/".$fname);
			}
			elseif($type == "gif"){
				$image = imagecreatefromgif("$dir/".$filename);
				imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
				imagejpeg($image_p,"$dir/".$newname, 180);
				if($fname != $newname) unlink("$dir/".$fname);				
			}
			elseif($type == "png"){
				$image = imagecreatefrompng("$dir/".$filename);
				imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
				imagejpeg($image_p,"$dir/".$newname, 180);
				if($fname != $newname) unlink("$dir/".$fname);				
			}
		}
		if($new_height > 80){
			$print_height = 80;	
			$print_width = 80 / $new_height * $new_width;
		} 
		else{
			$print_height = $new_height;	
			$print_widht = $new_width;
		} 
		$fname = $newname;
		chmod($dir."/".$newname,0644);
		$tag = $id;
		require_once('include.php');
		$q = "update partenaires set image = '$newname' where id = '$id'";
		ExecRequete($q);
		echo "<html><head>\n";
		echo "<script type = 'text/javascript'>\n";
		echo "var par = window.parent.document;\n";
		echo "var list = par.getElementById('photo_partenaire_$id');\n";
		echo "list.src = '$dir/$newname';\n";
		echo "list.setAttribute('width','$print_width');\n";
		echo "list.setAttribute('height','$print_height');\n";
		echo "</script>\n";
		echo "</head>\n";
		echo "</html>\n";
	}
	
    exit();
endif;
?>
<html><head>
<script language="javascript">
function upload(){
// hide old iframe
    var par = window.parent.document;
    var num = par.getElementsByTagName('iframe').length - 1;
    var iframe = par.getElementsByTagName('iframe')[num];
    iframe.className = 'hidden';

    // create new iframe
    var new_iframe = par.createElement('iframe');
    new_iframe.src = 'logo_partenaire_upload.php';
	new_iframe.style.width = '300px';
    new_iframe.frameBorder = '0';
    par.getElementById('iframe').appendChild(new_iframe);

    // add image progress
    // var list = par.getElementById('corps');
    // var new_div = par.createElement('div');
    // var new_img = par.createElement('img');
    // new_img.src = 'images/icon_indicator.gif';
    // new_img.className = 'load';
    // new_div.appendChild(new_img);
    // list.appendChild(new_div);
    // 
    // // send
    // var imgnum = list.getElementsByTagName('div').length - 1;
    // document.iform.imgnum.value = imgnum;
    document.iform.submit();
}
</script>
<style>
body {vertical-align:top;}
</style>
<link rel='stylesheet' href='styles/vbcll.css' type = 'text/css' />
<head>
<body style = 'background-color: #fff'>
	<div id = 'corps'>
<p class = 'center bold'>Modifier le logo</p>
<form name="iform" action="" method="post" enctype="multipart/form-data">
<p class = 'center'><input id="file" type="file" name="image" size = '10' onchange="upload()" />
<input type="hidden" name="imgnum" value = '1'/></p>
</form>
</div>
</body>
</html>